<?php
class TimesController extends AppController {

	var $name = 'Times';
	var $uses = array('Time', 'Task', 'Comment');
	var $components = array('Session', 'Auth', 'Date');
	
	function beforeFilter()
	{
		parent::beforeFilter();
		if ($this->Auth->user('role') == 'spectator')
		{
			$this->redirect('/');
		}
	}
	
	function play($task_id = null, $back_url = null)
	{
		if (!$task_id || !$back_url || !$this->check_permissions_for_task($this->Auth->user(), $task_id))
		{
			$this->redirect('/');
		}
		
		$task = $this->Task->read(null, $task_id);
		
		if ($task['Task']['status'] == 'closed')
		{
			$this->redirect('/');
		}
		
		$this->Time->recursive = -1;
		
		$time = $this->Time->find('first', array('conditions' => array(
			'Time.status' => 'pause',
			'Time.task_id' => $task_id,
			'Time.user_id' => $this->Auth->user('id')
		)));
		
		if (!empty($time))
		{
			$time['Time']['status'] = 'play';
			$time['Time']['pause'] = ( $time['Time']['pause'] + floor(time() / 60) - $time['Time']['finish'] ).'';
		}
		else
		{
			$time['Time'] = array();
			$time['Time']['user_id'] = $this->Auth->user('id');
			$time['Time']['task_id'] = $task_id;
			$time['Time']['start'] = (floor(time() / 60)).'';
			$time['Time']['finish'] = -1;
			$time['Time']['status'] = 'play';
		}
		
		$this->Time->save($time, false);
		
		$this->redirect('/'.base64_decode($back_url));
	}
	
	function stop($task_id = null, $back_url = null)
	{
		if (!$task_id || !$back_url || !$this->check_permissions_for_task($this->Auth->user(), $task_id))
		{
			$this->redirect('/');
		}
		
		$this->Time->recursive = -1;
		
		$time = $this->Time->find('first', array('conditions' => array(
			'Time.status !=' => 'stop',
			'Time.task_id' => $task_id,
			'Time.user_id' => $this->Auth->user('id')
		)));
		
		if (!empty($time))
		{
			switch($time['Time']['status'])
			{
				case 'play' :
				{
					$time['Time']['status'] = 'stop';
					$time['Time']['finish'] = (floor(time() / 60)).'';
					break;
				}
				case 'pause' :
				{
					$time['Time']['status'] = 'stop';
					$time['Time']['pause'] = ( $time['Time']['pause'] + floor(time() / 60) - $time['Time']['finish'] ).'';
					$time['Time']['finish'] = (floor(time() / 60)).'';
					break;
				}
			}
			
			$minutes = $time['Time']['finish'] - $time['Time']['start'] - $time['Time']['pause'];
			$hours = floor($minutes/60);
			$minutes = $minutes % 60;
			
			$hours = (strlen($hours) == 1) ? '0'.$hours : $hours;
			$minutes = (strlen($minutes) == 1) ? '0'.$minutes : $minutes;
			
			$this->Time->save($time, false);
			$this->Comment->save(array('Comment' => array(
				'user_id' => $this->Auth->user('id'),
				'task_id' => $task_id,
				'text' => $this->Auth->user('username').' report time - '.$hours.':'.$minutes
			)));
			$this->Task->set_modified_date($task_id);
			$this->redirect('/times/report/'.$time['Time']['id']);
		}
		
		$this->redirect('/');
	}
	
	function pause($task_id = null, $back_url = null) {
		if (!$task_id || !$back_url || !$this->check_permissions_for_task($this->Auth->user(), $task_id))
		{
			$this->redirect('/');
		}
		$this->Time->recursive = -1;
		$time = $this->Time->find('first', array('conditions' => array(
			'Time.status' => 'play',
			'Time.task_id' => $task_id,
			'Time.user_id' => $this->Auth->user('id')
		)));
		
		if (!empty($time))
		{
			$time['Time']['status'] = 'pause';
			$time['Time']['finish'] = (floor(time() / 60)).'';
			
			$this->Time->save($time, false);
			$this->redirect('/'.base64_decode($back_url));
		}
		
		$this->redirect('/');
	}
	
	function report($id = null)
	{
		
		if (!empty($id) || !empty($this->data['Time']['id']))
		{
			$time = $this->Time->find('first', array('conditions' => array('Time.id' => empty($id) ? $this->data['Time']['id'] : $id)));
			
			if (!empty($time))
			{
				
				if ($this->Auth->user('role') != 'admin' && $time['Time']['user_id'] != $this->Auth->user('id'))
				{
					$this->redirect('/');
				}
			}
			else
			{
				$this->redirect('/');
			}
		}
		else
		{
			$this->redirect('/');
		}
		
		if ($this->data)
		{
			$this->Time->set( $this->data );
			if ($this->Time->validates(array('fieldList' => array('start', 'finish', 'pause', 'comment')))) {
				$this->data['Time']['start'] = $this->Date->dateToMinutes($this->data['Time']['start']);
				$this->data['Time']['finish'] = $this->Date->dateToMinutes($this->data['Time']['finish']);
				$this->data['Time']['pause'] = $this->Date->timeToMinutes($this->data['Time']['pause']);

				$this->Time->save($this->data, array('validate' => false, 'fieldList' => array('start', 'finish', 'pause', 'comment')));
				
				$time = $this->Time->find('first', array('conditions' => array('Time.id' => $this->data['Time']['id'])));

				$this->redirect('/tasks/view/'.$time['Time']['task_id']);
			}
		}
		$this->data =$this->Time->find('first', array('conditions' => array('Time.id' => $id)));
		
		$this->data['Time']['start'] = $this->Date->minutesToDatetime($this->data['Time']['start']);
		$this->data['Time']['finish'] = $this->Date->minutesToDatetime($this->data['Time']['finish']);
		$this->data['Time']['pause'] = $this->Date->minutesToTime($this->data['Time']['pause']);
	}
}
